package com.kd.juhaowan.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kd.juhaowan.R;
import com.kd.juhaowan.http.api.SmallAccountGetApi;
import com.kd.juhaowan.http.api.SmallAccountSetApi;
import com.kd.juhaowan.http.model.HttpData;
import com.kd.juhaowan.http.model.HttpListData;
import com.kd.juhaowan.http.model.SmallAccountModel;
import com.kd.juhaowan.ui.adapter.BaseRecyclerViewAdapter;
import com.kd.juhaowan.ui.holder.SmallAccountViewHolder;
import com.kd.juhaowan.ui.listener.OnItemClickListener;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallbackProxy;
import com.hjq.toast.ToastUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SmallAccountActivity extends BaseActivity implements OnItemClickListener {





    @BindView(R.id.activity_account_user_account_lv)
    RecyclerView listView;



    private BaseRecyclerViewAdapter<SmallAccountViewHolder, SmallAccountModel> giftAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAdapter();
        gameId = getIntent().getStringExtra("game_id");
        if(getIntent().hasExtra("account_id")){
            userid = getIntent().getStringExtra("account_id");
        }
        EasyHttp.post(this).api(new SmallAccountGetApi(gameId)).request(new HttpCallbackProxy<HttpListData<SmallAccountModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<SmallAccountModel> result, boolean cache) {

                super.onHttpSuccess(result, cache);
                List<SmallAccountModel> accountModels = result.getData().getList();
                if(!TextUtils.isEmpty(userid)&&accountModels!=null){
                    for(int i=0;i<accountModels.size();i++){
                        if(userid.equals(accountModels.get(i).getTrumpet_user_id())){
                            selectIncex =i;
                        }
                    }
                }
                giftAdapter.resetAdapterData(result.getData().getList());

            }
        });
    }

    @Override
    protected void reInitDate() {
        EasyHttp.post(this).api(new SmallAccountGetApi(gameId)).request(new HttpCallbackProxy<HttpListData<SmallAccountModel>>(this){
            @Override
            public void onHttpSuccess(HttpListData<SmallAccountModel> result, boolean cache) {

                super.onHttpSuccess(result, cache);
                List<SmallAccountModel> accountModels = result.getData().getList();
                if(!TextUtils.isEmpty(userid)&&accountModels!=null){
                    for(int i=0;i<accountModels.size();i++){
                        if(userid.equals(accountModels.get(i).getTrumpet_user_id())){
                            selectIncex =i;
                        }
                    }
                }
                giftAdapter.resetAdapterData(result.getData().getList());

            }
        });
    }

    private String userid,gameId;
    private int selectIncex = -1;

    private void setAdapter(){
        giftAdapter = new BaseRecyclerViewAdapter<SmallAccountViewHolder, SmallAccountModel>() {
            @NonNull
            @Override
            public SmallAccountViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                LayoutInflater inflater = LayoutInflater.from(parent.getContext());
                View view = inflater.inflate(R.layout.item_smallaccount, null, false);
                SmallAccountViewHolder holder = new SmallAccountViewHolder(view);
                holder.setOnItemClickListener(getOnItemClickListener());
                return holder;
            }

            @Override
            public void onBindViewHolder(@NonNull SmallAccountViewHolder holder, int position) {
                holder.setDate(getItem(position),position==selectIncex);
            }
        };

        giftAdapter.setOnItemClickListener(this);
        giftAdapter.resetAdapterData(null);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);

        listView.setLayoutManager(layoutManager);
        listView.setAdapter(giftAdapter);
    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder, View view, int position) {
        userid = giftAdapter.getItem(position).getTrumpet_user_id();
        selectIncex= position;
        giftAdapter.notifyDataSetChanged();
    }

    @OnClick(R.id.login_confirm)
    public void onConfirmClick(){
        if(selectIncex==-1){
            ToastUtils.show("请先选择");
        }else {
            SmallAccountModel model = giftAdapter.getItem(selectIncex);

            EasyHttp.post(this).api(new SmallAccountSetApi(gameId,model.getId())).request(new HttpCallbackProxy<HttpData<SmallAccountModel>>(this){
                @Override
                public void onHttpSuccess(HttpData<SmallAccountModel> result, boolean cache) {

                    super.onHttpSuccess(result, cache);
                    Intent in =new Intent();
                    in.putExtra("model",model);
                    setResult(RESULT_OK,in);
                    finish();

                }
            });

        }
    }
    @OnClick(R.id.actvity_back)
    public void onBackClick(){
        finish();
    }

    @Override
    protected int getViewLayoutResourceId() {
        return R.layout.activity_smallaccount;
    }




}
